package edu.gatech.cs2340.group7.wheresmystuffapp.presenters;

import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.UnlockAccountView;

/**
 * Presenter for unlocking an account
 * 
 * @author tbowling3
 * 
 */
public class UnlockAccountPresenter {
	private UnlockAccountView myView;
	private DatabaseHelper myHelper;

	public UnlockAccountPresenter(UnlockAccountView view) {
		this.myView = view;
		myHelper = new DatabaseHelper(myView);
	}

	/**
	 * Handle the Unlock button from the UI view
	 * 
	 * @param username
	 */
	public void unlockAccount(String username) {
		if (username.isEmpty()) {
			myView.setResultText("Username invalid");
		} else if (!myHelper.accountExists(username)) {
			myView.setResultText("Username not Found");
		} else {
			Account change = myHelper.getAccount(username);
			if (change.isLocked()) {
				change.setLocked(false);
				myHelper.updateAccount(change);
				myView.setResultText("Account for " + username + " unlocked");
			}
			else {
				myView.setResultText("Account for " + username + "was already unlocked");
			} // close else
		} // close else
	} // close unlockAccount
} // close class
